import json
from config.configs import DATA_PATH, DESKTOP_PATH
from bson import ObjectId
import xlrd
import pandas as pd
from bson import objectid
import time
import requests


def deal_datas(result):
    """处理json文件中的$numberLong和$oid"""
    for i in range(len(result)):
        if "createdAt" in result[i] and type(result[i]["createdAt"]) == dict:
            result[i]["createdAt"] = float(result[i]["createdAt"]['$numberLong'])
        if "updatedAt" in result[i] and type(result[i]["updatedAt"]) == dict:
            result[i]["updatedAt"] = float(result[i]["updatedAt"]['$numberLong'])
        if "paidAt" in result[i] and type(result[i]["paidAt"]) == dict:
            result[i]["paidAt"] = float(result[i]["paidAt"]['$numberLong'])
        if "platformDeliveryAt" in result[i] and type(result[i]["platformDeliveryAt"]) == dict:
            result[i]["platformDeliveryAt"] = float(result[i]["platformDeliveryAt"]['$numberLong'])
        if "_id" in result[i] and type(result[i]["_id"]) == dict:
            result[i]["_id"] = ObjectId(result[i]["_id"]['$oid'])
        if "applicant" in result[i] and type(result[i]["applicant"]) == dict:
            result[i]["applicant"] = ObjectId(result[i]["applicant"]['$oid'])
        if "channel" in result[i] and type(result[i]["channel"]) == dict:
            result[i]["channel"] = ObjectId(result[i]["channel"]['$oid'])
        if "payment" in result[i]:
            li = []
            for z in result[i]["payment"]:
                x = ObjectId(z['$oid'])
                li.append(x)
            result[i]["payment"] = li
        if "estimatedPaymentAt" in result[i] and type(result[i]["estimatedPaymentAt"]) == dict:
            result[i]["estimatedPaymentAt"] = float(result[i]["estimatedPaymentAt"]['$numberLong'])
        if "paymentTime" in result[i] and type(result[i]["paymentTime"]) == dict:
            result[i]["paymentTime"] = float(result[i]["paymentTime"]['$numberLong'])
        if "paymentPassTime" in result[i] and type(result[i]["paymentPassTime"]) == dict:
            result[i]["paymentPassTime"] = float(result[i]["paymentPassTime"]['$numberLong'])
        if "salesPlanDayStartAt" in result[i] and type(result[i]["salesPlanDayStartAt"]) == dict:
            result[i]["salesPlanDayStartAt"] = float(result[i]["salesPlanDayStartAt"]['$numberLong'])
        if "salesPlanDayEndAt" in result[i] and type(result[i]["salesPlanDayEndAt"]) == dict:
            result[i]["salesPlanDayEndAt"] = float(result[i]["salesPlanDayEndAt"]['$numberLong'])
        if "preparationDayStartAt" in result[i] and type(result[i]["preparationDayStartAt"]) == dict:
            result[i]["preparationDayStartAt"] = float(result[i]["preparationDayStartAt"]['$numberLong'])
        if "preparationDayEndAt" in result[i] and type(result[i]["preparationDayEndAt"]) == dict:
            result[i]["preparationDayEndAt"] = float(result[i]["preparationDayEndAt"]['$numberLong'])
        if "submitTime" in result[i] and type(result[i]["submitTime"]) == dict:
            result[i]["submitTime"] = float(result[i]["submitTime"]['$numberLong'])
        if "confirmTime" in result[i] and type(result[i]["confirmTime"]) == dict:
            result[i]["confirmTime"] = float(result[i]["confirmTime"]['$numberLong'])
        if "deliveryEndTime" in result[i] and type(result[i]["deliveryEndTime"]) == dict:
            result[i]["deliveryEndTime"] = float(result[i]["deliveryEndTime"]['$numberLong'])
        if "deliveryStartTime" in result[i] and type(result[i]["deliveryStartTime"]) == dict:
            result[i]["deliveryStartTime"] = float(result[i]["deliveryStartTime"]['$numberLong'])
        if "createTimeAt" in result[i] and type(result[i]["createTimeAt"]) == dict:
            result[i]["createTimeAt"] = float(result[i]["createTimeAt"]['$numberLong'])
        if "payTimeAt" in result[i] and type(result[i]["confirmTime"]) == dict:
            result[i]["payTimeAt"] = float(result[i]["stmtTimeAt"]['$numberLong'])
        if "stmtTimeAt" in result[i] and type(result[i]["confirmTime"]) == dict:
            result[i]["stmtTimeAt"] = float(result[i]["stmtTimeAt"]['$numberLong'])
        if "transferTimeAt" in result[i] and type(result[i]["transferTimeAt"]) == dict:
            result[i]["transferTimeAt"] = float(result[i]["transferTimeAt"]['$numberLong'])
    return result


def read_json_file(file):
    """读取json文件"""
    with open(file, 'r', encoding='utf-8') as f:
        result = json.load(f)
        return deal_datas(result)


def read_xls_file(file, sheet_index, col_index):
    """读取xls文件指定列内容"""
    workbook = xlrd.open_workbook(file)
    open_sheet = workbook.sheet_by_index(sheet_index)
    col_values = open_sheet.col_values(col_index)
    return [col_values]


def read_xlsx_file(file, num):
    """读取xlsx文件指定列内容"""
    df = pd.read_excel(file)
    li = []
    for i in df.iloc[:, num]:
        li.append(i)
    return li


def create_balance_datas(currencyrate, channelName, currencytype, amount):
    """创建导入balances数据"""
    dic = dict()
    dic['_id'] = str(objectid.ObjectId())
    dic['currencyRate'] = currencyrate
    dic['channelName'] = channelName
    dic['__v'] = 0
    dic['amount'] = amount
    dic['currencyType'] = currencytype
    dic['createdAt'] = time.time()
    dic['updatedAt'] = time.time()
    return [dic["_id"], json.dumps(dic, ensure_ascii=False)]
